layui.use(['admin', 'table', 'form', 'tool', 'xmSelect'], function () {
	let $ = layui.$
		, admin = layui.admin
		, setter = layui.setter
		, view = layui.view
		, table = layui.table
		, form = layui.form
		, tool = layui.tool
		, controller = '/admin/order.index'; // 响应成功字段名;

	form.render(null, 'orderPage');

	form.on('switch(orderStatusSwitch)', function(data){
		let id = $(data.elem).attr("data-id");
		let status = 0;
		if(data.elem.checked){
			status = 1;
		}
		let field = {"id": id, "status": status};
		admin.req({
			url: setter.request.domainName + controller + '/editStatus' //实际使用请改成服务端真实接口
			, data: field
			, type: 'POST'
			, loading: false // 是否开启加载框
			, done: function (res) {

			}
		})
	});

	// 管理员管理
	table.render({
		elem: '#orderTable'
		, url: setter.request.domainName + controller + '/index?server=true' //模拟接口
		, toolbar: "#orderToolBar"
		, defaultToolbar: setter.defaultToolbar
		, page: true
		, cols: [[
			{type: 'checkbox', fixed: 'left'}
			, {field: 'id', title: 'ID', width: 80, sort: true}
			, {field: 'shop_name', width: 180, title: '店铺名称'}
			, {field: 'order_number', width: 200, title: '订单编号'}
			, {field: 'price', width: 100, title: '订单金额',
				templet: function(d) {
					return d.price + '元';
				}
			}
			, {field: 'cash', width: 100, title: '实付金额', 
				templet: function(d) {
					return d.cash + '元';
				}
			}
			, {field: 'source', width: 100, title: '来源',
				templet: function(d) {
					if(d.source == 1){
						return '淘宝';
					}else if(d.source == 2){
						return '京东';
					}else if(d.source == 3){
						return '抖音';
					}else if(d.source == 4){
						return '拼多多';
					}
					return '';
				}
			}
			, {field: 'order_time', width: 180, title: '下单时间', sort: true,
				templet: function(d) {
					return tool.timestampToTime(d.order_time);
				}
			}
			, {field: 'num', width: 120, title: '购买数量'}
			, {field: 'create_time', width: 180, title: '创建时间', sort: true,
				templet: function(d) {
					return tool.timestampToTime(d.create_time);
				}
			}
			, {title: '操作', width: 180, align: 'center', fixed: 'right', toolbar: '#orderTableTool'}
		]]
		, parseData: function(res){
			let code = res.code === 1 ? 0 : res.code;
			return {
				"code": code, // 解析接口状态
				"msg": res.msg, // 解析提示文本
				"count": res.data.total, // 解析数据长度
				"data": res.data.list // 解析数据列表
			};
		}
		, text: {none: '暂无数据！'}
	});

	// 自定义工具栏
	table.on('toolbar(orderTable)', function (obj) {
		let type = obj.event;
		active[type] ? active[type].call(this) : '';
	})

	// 工具条
	table.on('tool(orderTable)', function (obj) {
		let data = obj.data;
		if (obj.event === 'del') {
			layer.confirm('确定删除此数据？', function (index) {
				let ids = [];
				ids.push(obj.data.id);
				let field = {ids: ids};
				// 获取角色信息
				admin.req({
					url: setter.request.domainName + controller + '/del'
					, data: field
					, type: 'delete'
					, loading: false // 是否开启加载框
					, done: function () {
						obj.del();
						admin.message({
							title: '删除成功'
							,offset: '15px'
							,icon: 1
							,time: 1500
							,callback: function (){}
						})
					}
				})
				layer.close(index);
			});
		} else if (obj.event === 'edit') {
			admin.popup({
				title: '编辑数据'
				, area: ['500px', '750px']
				, id: 'order-popup-edit'
				, success: function (layero, index) {
					let layId = this.id;
					let field = {id: data.id};
					// 获取角色信息
					admin.req({
						url: setter.request.domainName + controller + '/edit'
						, data: field
						, type: 'get'
						, loading: false // 是否开启加载框
						, done: function (res) {
							let initData = res.data.row;
							initData['lay_index'] = index;
							view(layId).render('order/index/edit', initData).done(function () {
								form.render(null, 'layPopup');
							});
						}
					})
				}
			});
		}
	});

	//开启搜索
	form.on('submit(orderSearch)', function(data){
		let search = [];
		let searchArr = [];
		let nodeList = $(".orderSearchForm").find(".searchField");
		nodeList.each(function (i, e) {
			let searchData = {};
			let objNode = $(e);
			let val = objNode.val();
			if(!val && val !== 0  && val !== '0'){
				return true;
			}
			searchData['field'] = objNode.attr("name");
			searchData['val'] = val;
			searchData['operator'] = objNode.attr("operator");
			searchData['render'] = objNode.attr("render");
			search.push(searchData);
			searchArr['search'] = search;
		})
		// 执行重载
		table.reload('orderTable', {
			where: searchArr
		});
	});

	let active = {
		add: function() {
			admin.popup({
				title: '添加数据'
				,area: ['500px', '750px']
				,id: 'order-data-popup-add'
				,success: function(layero, index){
					view(this.id).render('order/Index/add', {lay_index : index}).done(function(){
						// 渲染表单
						form.render(null, 'layPopup');
					});
				}
			});
		}
		,batchDel: function() {
			let checkStatus = table.checkStatus('orderTable')
				, checkData = checkStatus.data // 得到选中的数据
				, ids = []; // ID

			if(checkData.length === 0){
				return layer.msg('请选择要删除的数据');
			}
			$.each(checkData, function (i, val){
				ids.push(val.id);
			})
			layer.confirm('确定删除选中的数据？', function (index) {
				let field = {ids: ids};
				// 获取角色信息
				admin.req({
					url: setter.request.domainName + controller + '/del'
					, data: field
					, type: 'delete'
					, loading: false // 是否开启加载框
					, done: function () {
						table.reload('orderTable');
						admin.message({
							title: '删除成功'
							,offset: '15px'
							,icon: 1
							,time: 1500
							,callback: function (){}
						})
					}
				})
				layer.close(index);
			});
		}
	}
})
